import express from 'express'
import mysql from 'mysql2/promise'
import { dbConfig } from '../config/database.js'

const router = express.Router()

// 获取所有轮次类型
router.get('/', async (req, res) => {
  try {
    const connection = await mysql.createConnection(dbConfig)
    const [rows] = await connection.query('SELECT * FROM round_types')
    await connection.end()
    res.json(rows)
  } catch (error) {
    res.status(500).json({ error: error.message })
  }
})

// 创建新轮次类型
router.post('/', async (req, res) => {
  try {
    const { name, description } = req.body
    const connection = await mysql.createConnection(dbConfig)
    const [result] = await connection.query(
      'INSERT INTO round_types (name, description) VALUES (?, ?)',
      [name, description]
    )
    await connection.end()
    res.json({ id: result.insertId })
  } catch (error) {
    res.status(500).json({ error: error.message })
  }
})

export default router 